package com.xiaoshi.utils;

import com.xiaoshi.enums.JsonReturnEnums;
import com.xiaoshi.exception.PlatformException;
import com.xiaoshi.interfaces.MyHttpServiceCode;


import java.util.Map;

public class JsonResult<T> {
    private Integer status;
    private String msg;
    private T data;

    public JsonResult() {
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }




    public static <T> JsonResult<T> fail() {
        JsonResult<T> ret = new JsonResult<T>();
        ret.setStatus(JsonReturnEnums.FAIL.getCode());
        ret.setMsg(JsonReturnEnums.FAIL.getDesc());
        return ret;
    }

    public static <T> JsonResult<T> fail(T data) {
        JsonResult<T> ret = JsonResult.fail();
        ret.setMsg(String.valueOf(data));
        return ret;
    }

    public static <T> JsonResult<T> fail(JsonReturnEnums jsonReturnEnums) {
        JsonResult<T> ret = JsonResult.fail();
        ret.setStatus(jsonReturnEnums.getCode());
        ret.setMsg(jsonReturnEnums.getDesc());
        return ret;
    }


    /**
     * 构建返回的JSON数据格式
     *
     * @param jsonReturnEnums
     * @return
     */
    public static Map<String, Object> makeJsonByEnums(JsonReturnEnums jsonReturnEnums) {

//        map.put(Constants.myConstans.CODE, jsonReturnEnums.getCode());
//        map.put(Constants.myConstans.MSG, jsonReturnEnums.getDesc());
//        map.put(Constants.myConstans.DATA, null);
        return  MapUtils.returnEnumsData(jsonReturnEnums);
    }


    public static <T> JsonResult<T> fail(Integer code, String msg) {
        JsonResult<T> ret = new JsonResult<T>();
        ret.setStatus(code);
        ret.setMsg(msg);
        return ret;
    }

    public static <T> JsonResult<T> failFormat(JsonReturnEnums jsonReturnEnums ,String ...msg) {
        JsonResult<T> ret = JsonResult.fail();
        ret.setStatus(jsonReturnEnums.getCode());
        ret.setMsg(String.format(jsonReturnEnums.getDesc(),msg) );
        return ret;
    }

    //Exception

    public static <T> JsonResult<T> failFormat(String msg ,String ...FormatMsg) {
        JsonResult<T> ret = JsonResult.fail();
        ret.setStatus(MyHttpServiceCode.AMIS_FAIL);
        ret.setMsg(String.format(msg,FormatMsg) );
        return ret;
    }





    public static <T> JsonResult<T> returnJsonResultByInteger(Integer num, String failMsg ) {
        if(num == null) throw new PlatformException( "JsonResult num 为空");
        if(num > IntegerUtils.SIZO ){  // 成功
            return success();
        }else {
            return failMessage(failMsg);
        }
    }


    public static <T> JsonResult<T> returnJsonResultByInteger(Integer num, String successMsg, String failMsg ) {
        if(num == null) throw new PlatformException( "JsonResult num 为空");
        if(num > IntegerUtils.SIZO ){  // 成功
            return successMessage(successMsg);
        }else {
            return failMessage(failMsg);
        }
    }

    public static <T> JsonResult<T> returnJsonResultByBoolean(Boolean bool, String successMsg, String failMsg ) {
        if(bool == null) throw new PlatformException( "JsonResult bool 为空");
        if(bool){  // 成功
            return successMessage(successMsg);
        }else {
            return failMessage(failMsg);
        }
    }


    public static <T> JsonResult<T> returnJsonResultByBoolean(Boolean bool, String failMsg ) {
        if(bool == null) throw new PlatformException( "JsonResult bool 为空");
        if(bool){  // 成功
            return success();
        }else {
            return failMessage(failMsg);
        }
    }







    public static JsonResult success() {
        JsonResult ret = new JsonResult();
        ret.setStatus(JsonReturnEnums.SUCCESS.getCode());
        ret.setMsg(JsonReturnEnums.SUCCESS.getDesc());
        //   ret.setData(JsonReturnEnums.SUCCESS.getDesc());
        return ret;
    }


    public static <T> JsonResult<T> success(T data) {
        JsonResult<T> ret = JsonResult.success();
        ret.setStatus(JsonReturnEnums.SUCCESS.getCode());
        ret.setMsg(JsonReturnEnums.SUCCESS.getDesc());
        ret.setData(data);
        return ret;
    }


    public static <T> JsonResult<T> successMessage(String message) {
        JsonResult ret = new JsonResult();
        ret.setStatus(JsonReturnEnums.SUCCESS.getCode());
        ret.setMsg(message);
        return ret;
    }

    public static <T> JsonResult<T> failMessage(String message) {
        JsonResult ret = new JsonResult();
        ret.setStatus(JsonReturnEnums.FAIL.getCode());
        ret.setMsg(message);
        return ret;
    }



}
